<?php
$url=$_GET['url'];
header('Content-Type:application/json');
if(!$url){
$player['code']=404;
$player['msg']='请输入地址';
echo json_encode($player, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
if (!is_dir('clydm/')) mkdir('clydm/');
$url = str_replace('http://','https://',$url);
	$ep_file= 'clydm/'.md5($url).'.m3u8';
      //time()
	if (!file_exists($ep_file)){//3600= 1小时  缓存时间

	echo file_get_contents(get_maom3u8_url($url));
	}else{
$zurl = 'https://'.$_SERVER['HTTP_HOST'].'/'.$ep_file;
        

		echo file_get_contents($zurl);

}

function get_maom3u8_url($url){
    $content = file_get_contents($url);
    preg_match('#data-vid="(\w+)" data-cid="(\w+)"#iU',$content,$id);
    $vid = $id[1];
    $DATA=file_get_contents("https://dm.video.qq.com/barrage/segment/$vid/t/v1/180000/210000");//https://dmku.thefilehosting.com/?ac=dm&id=
	$DATA=json_decode($DATA,true);
		$pcdm = array();
		$pcdm[] = array(
			        0,
				    'top',
				    $rgb,
				    "",
				    "有 ".count($DATA['barrage_list'])." 条弹幕列队来袭~做好准备吧！",
				    "",
                    "",
                    "24px"
				);
		foreach($DATA['barrage_list'] as $subArray) {
			$randomColor = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
			// 随机生成RGB颜色代码
			$rgb = 'rgb('.mt_rand(0, 255).', '.mt_rand(0, 255).', '.mt_rand(0, 255).')';
			// 生成随机的RGB颜色参数
			$pcdm[] = array(
			        0,
				    'top',
				    $rgb,
				    "",
				    $subArray['content'],
				    "",
                    "",
                    "24px"
				);
		}
$arr['code']=23;
		$arr['msg']="弹幕获取成功";
		$arr['name']=$url;
		$arr['danmu']=count($DATA['barrage_list']);
		$arr['danmuku']=$pcdm;
		$pcdm=json_encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    $ep_file= 'clydm/'.md5($url).'.m3u8';
                file_put_contents($ep_file,$pcdm);
                $zurl ='https://'.$_SERVER['HTTP_HOST'].'/'.$ep_file;
                return $zurl;
}